package com.avic.rong.fragment;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.ViewPager;

import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.api.HttpPostService;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.example.minato.minastore.utils.AppUtils;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * A simple {@link Fragment} subclass.
 */
//我的消息
public class FragmentMyMessage extends BaseFragment {

    @BindView(R.id.recycler_msg)
    ViewPager mVp;
    @BindView(R.id.tablayout_msg)
    SlidingTabLayout mTabLayout;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles = {"账户通知", "客服消息"};
    private MyPagerAdapter myPagerAdapter;

    private int keFuCount;
    private int REFRESH = 200;

    public FragmentMyMessage() {
        // Required empty public constructor
    }



    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(getActivity());
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.CART_BROADCAST");
        BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent){
                String msg = intent.getStringExtra("data");
                if("refresh".equals(msg)){
                    refresh();
                }
            }
        };
        broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter);
    }

    private void refresh() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(AppUtils.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        HttpPostService httpPostService = retrofit.create(HttpPostService.class);

        Observable<Double> observable = httpPostService.unReadKeFuMessageCount(SharedPreferencesUtil.getLoginToken());

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Double>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Double aDouble) {
                        keFuCount =  aDouble.intValue();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getKeFuCount(int keFuCount){
        this.keFuCount = keFuCount;
    }

  /*  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_my_message,container,false);

        // Inflate the layout for this fragment
//        View inflate = inflater.inflate(R.layout.fragment_my_message, container, false);
        ButterKnife.bind(this, view);


        mFragments.add(new TradingFragment());
        //  mFragments.add(new AccountNoticeFragment());
        mFragments.add(new ServiceNewsFragment());
        myPagerAdapter = new MyPagerAdapter(getChildFragmentManager());
        mVp.setAdapter(myPagerAdapter);
        mTabLayout.setViewPager(mVp);
        mVp.setCurrentItem(0);
        mVp.setOffscreenPageLimit(1);


        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mTabLayout.hideMsg(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });


        if (keFuCount>0){
            mTabLayout.showDot(1);
        }else {
            mTabLayout.hideMsg(1);
        }

        return view;

    }*/

    @Override
    protected void initView() {
//        view=inflater.inflate(R.layout.fragment_my_message,container,false);
//
//        // Inflate the layout for this fragment
////        View inflate = inflater.inflate(R.layout.fragment_my_message, container, false);
//        ButterKnife.bind(this, view);


        mFragments.add(new TradingFragment());
        //  mFragments.add(new AccountNoticeFragment());
        mFragments.add(new ServiceNewsFragment());
        myPagerAdapter = new MyPagerAdapter(getChildFragmentManager());
        mVp.setAdapter(myPagerAdapter);
        mTabLayout.setViewPager(mVp);
        mVp.setCurrentItem(0);
        mVp.setOffscreenPageLimit(1);





        if (keFuCount>0){
            mTabLayout.showDot(1);
        }else {
            mTabLayout.hideMsg(1);
        }

//        return view;
    }

    @Override
    protected void addListener() {
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mTabLayout.hideMsg(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_my_message;
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }


    }

}
